package team.archai.nexus.boot.tool;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


/**
 * @description: 工具类
 * @author: lishibin
 * @create: 2020-06-26 14:27
 **/
public class Tool {

    private static final Set<Class<?>> BasicsClass = new HashSet<Class<?>>() {
        {
            add(Integer.class);
            add(String.class);
            add(Double.class);
            add(Long.class);
            add(Date.class);
            add(int.class);
            add(double.class);
            add(long.class);
        }
    };

    private static final Pattern linePattern = Pattern.compile("_(\\w)");


    private static final Pattern letterPattern = Pattern.compile("[A-Z]");

    /**
     * @program: Tool
     * @description: 获取uuid
     * @author: lishibin
     * @create: 9:30 PM
     **/
    public static String uuid() {
        return StrUtil.uuid();
    }

    /**
     * @program: Tool
     * @description: 对象转int 默认值0
     * @author: lishibin
     * @create: 9:30 PM
     **/
    public static int toInt(Object intStr) {
        return toInt(intStr, 0);
    }

    /**
     * @program: Tool
     * @description: 对象转int 设置默认值
     * @author: lishibin
     * @create: 9:29 PM
     **/
    public static int toInt(Object intStr, int defaultValue) {
        if (Objects.isNull(intStr)) {
            return defaultValue;
        }
        return Integer.parseInt(String.valueOf(intStr));
    }

    /**
     * @program: Tool
     * @description: 对象转string 默认值为空字符串
     * @author: lishibin
     * @create: 9:29 PM
     **/
    public static String toStr(Object obj) {
        return toStr(obj, "");
    }

    /**
     * @program: Tool
     * @description: 对象转string 默认值
     * @author: lishibin
     * @create: 9:29 PM
     **/
    public static String toStr(Object obj, String defaultValue) {
        return Objects.isNull(obj) ? defaultValue : String.valueOf(obj);
    }

    /**
     * @program: Tool
     * @description: 判断对象是否为空
     * @author: lishibin
     * @create: 9:29 PM
     **/
    public static boolean isEmpty(Object obj) {
        if (obj instanceof String) {
            return StrUtil.isEmpty((String) obj);
        } else if (obj instanceof Collection) {
            return CollectionUtil.isEmpty((Collection) obj);
        } else if (obj instanceof Map) {
            return MapUtil.isEmpty((Map) obj);
        } else {
            return obj == null;
        }
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * @program: Tool
     * @description: 判断对象是否不为空
     * @author: lishibin
     * @create: 9:28 PM
     **/
    public static boolean notEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * @program: Tool
     * @description: 对象转long
     * @author: lishibin
     * @create: 9:28 PM
     **/
    public static long toLong(Object obj, long defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        return Long.parseLong(String.valueOf(obj));
    }

    /**
     * 转换为float
     * @param value
     * @param defaultValue
     * @return
     */
    public static Float toFloat(Object value, Float defaultValue) {
        if (value != null) {
            return Float.valueOf(String.valueOf(value).trim());
        }
        return defaultValue;
    }

    /**
     * 转换为Double
     * @param value
     * @param defaultValue
     * @return
     */
    public static Double toDouble(Object value, Double defaultValue) {
        if (value != null) {
            return Double.valueOf(String.valueOf(value).trim());
        }
        return defaultValue;
    }

    /**
     * 转换为boolean
     * @param value
     * @param defaultValue
     * @return
     */
    public static Boolean toBoolean(Object value, Boolean defaultValue) {
        if (value != null) {
            String val = String.valueOf(value);
            val = val.toLowerCase().trim();
            return Boolean.parseBoolean(val);
        }
        return defaultValue;
    }

    /**
     * @program: Tool
     * @description: 对象转long
     * @author: lishibin
     * @create: 9:28 PM
     **/
    public static long toLong(Object obj) {
        return toLong(obj, 0L);
    }

    /**
     * @program: Tool
     * @description: list 转数组
     * @author: lishibin
     * @create: 9:28 PM
     **/
    public static String[] toArray(List<String> stringList) {
        if (null == stringList) {
            return null;
        }
        return stringList.stream().toArray(String[]::new);
    }

    /**
     * @program: Tool
     * @description: 判断对象是否是基础数据类型
     * @author: lishibin
     * @create: 9:27 PM
     **/
    public static boolean isBasicsType(Object obj) {
        return isBasicsType(obj.getClass());
    }

    /**
     * @program: Tool
     * @description: 判断是否是基础数据类型
     * @author: lishibin
     * @create: 7:29 PM
     **/
    public static boolean isBasicsType(Class clazz) {
        if (null == clazz) {
            return false;
        }
        Optional<?> cls = BasicsClass.stream().filter(b -> b.equals(clazz)).findFirst();
        return cls.isPresent();
    }


    /***
     * 转换成小写蛇形命名
     * @param str
     * @return
     */
    public static String hump2Line(String str) {
        Matcher matcher = letterPattern.matcher(str);
        // 大小写混合，则遇“大写”转换为“_小写”
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @program: StringUtil
     * @description: 下划线转驼峰
     * @author: lishibin
     * @create: 11:02 AM
     **/
    public static String line2Hump(String str) {
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @program: Tool
     * @description:
     * @author: lishibin
     * @create: 9:47 PM
     **/
    public static List<Field> annotationFields(Class<?> clazz, Class<? extends Annotation> annotationCls) {
        Field[] fields = ReflectUtil.getFields(clazz);
        return Arrays.stream(fields).filter(field -> field.isAnnotationPresent(annotationCls))
                .collect(Collectors.toList());
    }


    /**
     * 转换为String集合<br>
     *
     * @param split 分隔符
     * @param str   被转换的值
     * @return 结果
     */
    public static List<String> toStrList(String split, String str) {
        return StrUtil.split(str, split);
    }

}
